package com.administrator.scoreManageSys.service.impl;

import com.administrator.scoreManageSys.entity.system.Permission;
import com.administrator.scoreManageSys.mapper.system.PermissionsMenuMapper;
import com.administrator.scoreManageSys.service.PermissionsMenuService;
import com.administrator.scoreManageSys.service.RoleService;
import com.administrator.scoreManageSys.utils.ResultUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 权限菜单管理
 *
 * @author Administrator
 */
@Service
public class PermissionsMenuServiceImpl implements PermissionsMenuService {

    @Resource
    private PermissionsMenuMapper permissionsMenuMapper;
    @Resource
    private ResultUtils resultUtils;
    @Resource
    private RoleService roleService;

    /**
     * 获取菜单列表
     *
     * @return 菜单列表
     */
    @Override
    public JSONObject menuList() {
        List<Permission> permissions = permissionsMenuMapper.menuList();
        return resultUtils.resultSuccessData(permissions);
    }

    /**
     * 添加菜单
     *
     * @param permission   权限关键字符串
     * @param menuName     菜单名称
     * @param route        路由地址
     * @param icon         图标
     * @param parent       父级 1级菜单父级为0
     * @param serialNumber 序号
     * @param grade        权限等级 1/2/3 一级/二级/三级 权限
     * @param menuOpend    是否开启菜单 1/0 开启/关闭
     * @return 操作结果
     */
    @Override
    public JSONObject menuAdd(String permission, String menuName, String route, String icon, Long parent, Long serialNumber, Long grade, Long menuOpend) {
        permissionsMenuMapper.menuAdd(permission, menuName, route, icon, parent, serialNumber, grade, menuOpend);
        //格式化角色权限及菜单到redis
        roleService.formatRolePermissionAndMenu();
        return resultUtils.resultSuccess();
    }

    /**
     * 修改菜单
     *
     * @param id           修改的菜单id
     * @param permission   权限关键字符串
     * @param menuName     菜单名称
     * @param route        路由地址
     * @param icon         图标
     * @param parent       父级 1级菜单父级为0
     * @param serialNumber 序号
     * @param grade        权限等级 1/2/3 一级/二级/三级 权限
     * @param menuOpend    是否开启菜单 1/0 开启/关闭
     * @return 操作结果
     */
    @Override
    public JSONObject menuUpdate(Long id, String permission, String menuName, String route, String icon, Long parent, Long serialNumber, Long grade, Long menuOpend) {
        permissionsMenuMapper.menuUpdate(id, permission, menuName, route, icon, parent, serialNumber, grade, menuOpend);
        //格式化角色权限及菜单到redis
        roleService.formatRolePermissionAndMenu();
        return resultUtils.resultSuccess();
    }

    /**
     * 删除菜单
     *
     * @param id 删除菜单id
     * @return 操作结果
     */
    @Override
    public JSONObject menuDelete(Long id) {
        //获取删除菜单的所有子菜单
        List<Permission> menuJSONObjectIds = permissionsMenuMapper.getMenuIds(id);
        List<Integer> menuIds = new ArrayList<>();
        menuJSONObjectIds.forEach(str -> {
            menuIds.add(Integer.valueOf(str.getId().toString()));
            if (str.getChildrenPermission().size() > 0) {
                str.getChildrenPermission().forEach(st -> {
                    menuIds.add(Integer.valueOf(st.getId().toString()));
                    if (st.getChildrenPermission().size() > 0) {
                        st.getChildrenPermission().forEach(s -> {
                            menuIds.add(Integer.valueOf(s.getId().toString()));
                        });
                    }
                });
            }
        });
        //删除菜单
        if (menuIds.size() > 0) {
            permissionsMenuMapper.menuDelete(menuIds);
        }
        //删除菜单关联角色数据
        permissionsMenuMapper.roleMenuDelete(id);
        //格式化角色权限及菜单到redis
        roleService.formatRolePermissionAndMenu();
        return resultUtils.resultSuccess();
    }

    /**
     * 获取父级菜单列表
     *
     * @return 操作结果
     */
    @Override
    public JSONObject getParentList() {
        JSONArray result = permissionsMenuMapper.getParentList();
        return resultUtils.resultSuccessData(result);
    }


}
